﻿define(function (require) {
    var WorkitemView = require('view/WorkitemView');
    var service = require('ezdesk/utility/service');

    return Backbone.View.extend({
        el: $(".workitems"),
        events: {
    },

    initialize: function () {
        _.bindAll(this, 'addOne', 'addAll', 'render');
        this.model.bind('add', this.render);
        this.model.bind('remove', this.render);
        this.model.bind('change', this.render);
    },

    render: function () {
        $(".workitems").empty();
        this.addAll();

        this.model.bind('message', function (statusInfo) {
            $.postMessage("message:" + statusInfo.Info, "*", parent);
        });


        var workitemCount = this.model.models.length;
        $.postMessage('workitems:' + workitemCount, '*', parent);

        if (workitemCount == 0) this.trigger("empty");
    },
    addOne: function (e) {
        var view = new WorkitemView({ model: e });
        var element = view.render().el;
        this.el.append(element);

        $(this.el).find('.command').show();
        $(this.el).find('.img-ajax').hide();
    },

    addAll: function () {
        this.model.each(this.addOne);
    },
    clear: function (e) {

    }

});

});